package com.ymatou.iapi.payment.testcase;


import java.util.ArrayList;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.payment.parameter.AcquireOrderBean;
import com.ymatou.iapi.payment.parameter.ApproveRefundBean;
import com.ymatou.iapi.payment.parameter.SubmitRefundBean;
import com.ymatou.iapi.payment.service.ApproveRefundCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.PayGateWayCallService;
import com.ymttest.database.model.MRefund;
import com.ymttest.database.sqlwapper.PayGateWayWapper;

/**
 * 退款申请审核通过接口
 * FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_ApproveRefund {
    private static ApproveRefundBean approverefundBean;
    private static ApproveRefundCall approverefundCall;

    @BeforeClass
    public static void setUp() {
        Logger.createResultFile("退款申请审核通过接口");
    }

    @Before
    public void caseUp() {
        approverefundBean = new ApproveRefundBean();
        approverefundCall = new ApproveRefundCall();
    }

    @AfterClass
    public static void tearDown() {
        Logger.generateResult(approverefundCall.getOpurl());
    }

    @After
    public void caseDown() {
        Logger.end();
    }

    @Test
    @Category({P3.class})
    @TestCase
    public void Tc_ApproveRefund_001() {
        Logger.start(true, "支付宝审核退款成功");
        try {
            AcquireOrderBean aqb = PayGateWayCallService.initAcquireOrder("10");
            PayGateWayCallService.acquireOrder(aqb);
            PayGateWayCallService.notifyCall(aqb);
            SubmitRefundBean submitrefundBean = PayGateWayCallService.JSubmitRefund(aqb);
            MRefund m=new PayGateWayWapper().selectRefundRequestByTraceId(submitrefundBean.getTraceId()).get(0);
            String refundId = m.getRefundId().toString();
            approverefundBean.setApproveUser("AutoTest");
            List<String> refundIds = new ArrayList<String>();
            refundIds.add(refundId);
            approverefundBean.setRefundIds(refundIds);

            approverefundCall.setData(approverefundBean);
            approverefundCall.callService();
            Logger.verifyEquals("true",approverefundCall.getString("IsSuccess"),"验证IsSuccess");
            Logger.verifyEquals("0",approverefundCall.getString("ErrorCode"),"验证ErrorCode");
            verifyDB(approverefundBean);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category({P3.class})
    @TestCase
    public void Tc_ApproveRefund_002() {
        Logger.start(true, "不输入PaymentIds审核退款失败");
        try {
            // AcquireOrderBean aqb = PayGateWayCallService.initAcquireOrder("10");
            // PayGateWayCallService.acquireOrder(aqb);
            // PayGateWayCallService.notifyCall(aqb);
            // PayGateWayCallService.submitRefund(aqb);
            // String pid = PayGateWayCallService.getPaymentId(aqb);
            approverefundBean.setApproveUser("AutoTest");
            // List<String> PaymentIds=new ArrayList<String>();
            // PaymentIds.add(pid);
            // approverefundBean.setPaymentIds(PaymentIds);
            approverefundCall.setData(approverefundBean);
            approverefundCall.callService();
            Logger.verifyEquals("1000", approverefundCall.getString("ErrorCode"), "验证ErrorCode");
            Logger.verifyEquals("refundIds not be empty", approverefundCall.getString("ErrorMessage"), "验证ErrorMessage");
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category({P3.class})
    @TestCase
    public void Tc_ApproveRefund_003() {
        Logger.start(true, "不输入ApproveUses审核退款失败");
        try {
            AcquireOrderBean aqb = PayGateWayCallService.initAcquireOrder("10");
            PayGateWayCallService.acquireOrder(aqb);
            PayGateWayCallService.notifyCall(aqb);
            PayGateWayCallService.submitRefund(aqb);
            String pid = PayGateWayCallService.getPaymentId(aqb);
            // approverefundBean.setApproveUser("AutoTest");
            List<String> refundNos = new ArrayList<String>();
            refundNos.add(pid);
            approverefundBean.setRefundIds(refundNos);
            approverefundCall.setData(approverefundBean);
            approverefundCall.callService();
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category({P3.class})
    @TestCase
    public void Tc_ApproveRefund_004() {
        Logger.start(true, "输入不存在RefundNos审核退款失败");
        try {
            String pid = "000000";
            approverefundBean.setApproveUser("AutoTest");
            List<String> refundNos = new ArrayList<String>();
            refundNos.add(pid);
            approverefundBean.setRefundIds(refundNos);
            approverefundCall.setData(approverefundBean);
            approverefundCall.callService();
            // FIXME code
            Logger.verifyEquals("3103", approverefundCall.getString("ErrorCode"), "验证ErrorCode");
            Logger.verifyIsContains("退款单号不存在|refund request not exist.",
                    approverefundCall.getString("ErrorMessage"), "验证ErrorMessage");
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category({P3.class})
    @TestCase
    public void Tc_ApproveRefund_005() {
        Logger.start(true, "PaymentIds传入空列表审核退款失败");
        try {
            // AcquireOrderBean aqb = PayGateWayCallService.initAcquireOrder("10");
            // PayGateWayCallService.acquireOrder(aqb);
            // PayGateWayCallService.notifyCall(aqb);
            // PayGateWayCallService.submitRefund(aqb);
            // String pid = PayGateWayCallService.getPaymentId(aqb);
            approverefundBean.setApproveUser("AutoTest");
            List<String> refundNos = new ArrayList<String>();
            // PaymentIds.add(pid);
            approverefundBean.setRefundIds(refundNos);
            approverefundCall.setData(approverefundBean);
            approverefundCall.callService();
            Logger.verifyEquals("1000", approverefundCall.getString("ErrorCode"), "验证ErrorCode");
            Logger.verifyEquals("refundIds not be empty", approverefundCall.getString("ErrorMessage"), "验证ErrorMessage");
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category({P3.class})
    @TestCase
    public void Tc_ApproveRefund_006() {
        Logger.start(true, "审核多笔退款成功");
        try {
        	 AcquireOrderBean aqb = PayGateWayCallService.initAcquireOrder("10");
             PayGateWayCallService.acquireOrder(aqb);
             PayGateWayCallService.notifyCall(aqb);
             SubmitRefundBean submitrefundBean = PayGateWayCallService.JSubmitRefund(aqb);
             MRefund m=new PayGateWayWapper().selectRefundRequestByTraceId(submitrefundBean.getTraceId()).get(0);
             String RefundId1 = m.getRefundId().toString();
            
             AcquireOrderBean aqb2 = PayGateWayCallService.initAcquireOrder("10");
             PayGateWayCallService.acquireOrder(aqb2);
             PayGateWayCallService.notifyCall(aqb2);
             SubmitRefundBean submitrefundBean2 = PayGateWayCallService.JSubmitRefund(aqb2);
             MRefund m2=new PayGateWayWapper().selectRefundRequestByTraceId(submitrefundBean2.getTraceId()).get(0);
             String RefundId2 = m2.getRefundId().toString();
             
             approverefundBean.setApproveUser("AutoTest");
             List<String> refundIds = new ArrayList<String>();
             refundIds.add(RefundId1);
             refundIds.add(RefundId2);
             approverefundBean.setRefundIds(refundIds);
             
             approverefundBean.setApproveUser("AutoTest");
             
            approverefundCall.setData(approverefundBean);
            approverefundCall.callService();
            Logger.verifyEquals("0", approverefundCall.getString("ErrorCode"), "验证ErrorCode");
            Logger.verifyEquals("null", approverefundCall.getString("ErrorMessage"), "验证ErrorMessage");
            verifyDB(approverefundBean);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }
    @Test
    @Category({P3.class})
    @TestCase
    public void Tc_ApproveRefund_007() {
        Logger.start(true, "微信审核退款成功");
        try {
            AcquireOrderBean aqb = PayGateWayCallService.initAcquireOrder("14");
            PayGateWayCallService.acquireOrder(aqb);
            PayGateWayCallService.notifyCall(aqb);
            SubmitRefundBean submitrefundBean = PayGateWayCallService.JSubmitRefund(aqb);
            MRefund m=new PayGateWayWapper().selectRefundRequestByTraceId(submitrefundBean.getTraceId()).get(0);
            String refundId = m.getRefundId().toString();
            approverefundBean.setApproveUser("AutoTest");
            List<String> refundIds = new ArrayList<String>();
            refundIds.add(refundId);
            approverefundBean.setRefundIds(refundIds);

            approverefundCall.setData(approverefundBean);
            approverefundCall.callService();
            Logger.verifyEquals("true",approverefundCall.getString("IsSuccess"),"验证IsSuccess");
            Logger.verifyEquals("0",approverefundCall.getString("ErrorCode"),"验证ErrorCode");
            verifyDB(approverefundBean);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }
    @Test
    @Category({P3.class})
    @TestCase
    public void Tc_ApproveRefund_008() {
        Logger.start(true, "招商审核退款成功");
        try {
            AcquireOrderBean aqb = PayGateWayCallService.initAcquireOrder("50");
            PayGateWayCallService.acquireOrder(aqb);
            PayGateWayCallService.notifyCall(aqb);
            SubmitRefundBean submitrefundBean = PayGateWayCallService.JSubmitRefund(aqb);
            MRefund m=new PayGateWayWapper().selectRefundRequestByTraceId(submitrefundBean.getTraceId()).get(0);
            String refundId = m.getRefundId().toString();
            approverefundBean.setApproveUser("AutoTest");
            List<String> refundIds = new ArrayList<String>();
            refundIds.add(refundId);
            approverefundBean.setRefundIds(refundIds);

            approverefundCall.setData(approverefundBean);
            approverefundCall.callService();
            Logger.verifyEquals("true",approverefundCall.getString("IsSuccess"),"验证IsSuccess");
            Logger.verifyEquals("0",approverefundCall.getString("ErrorCode"),"验证ErrorCode");
            verifyDB(approverefundBean);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }
    @Test
    @Category({P3.class})
    @TestCase
    public void Tc_ApproveRefund_009() {
        Logger.start(true, "applepay审核退款成功");
        try {
            AcquireOrderBean aqb = PayGateWayCallService.initAcquireOrder("60");
            PayGateWayCallService.acquireOrder(aqb);
            PayGateWayCallService.notifyCall(aqb);
            SubmitRefundBean submitrefundBean = PayGateWayCallService.JSubmitRefund(aqb);
            MRefund m=new PayGateWayWapper().selectRefundRequestByTraceId(submitrefundBean.getTraceId()).get(0);
            String refundId = m.getRefundId().toString();
            approverefundBean.setApproveUser("AutoTest");
            List<String> refundIds = new ArrayList<String>();
            refundIds.add(refundId);
            approverefundBean.setRefundIds(refundIds);

            approverefundCall.setData(approverefundBean);
            approverefundCall.callService();
            Logger.verifyEquals("true",approverefundCall.getString("IsSuccess"),"验证IsSuccess");
            Logger.verifyEquals("0",approverefundCall.getString("ErrorCode"),"验证ErrorCode");
            verifyDB(approverefundBean);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }
    @Test
    @Category({P3.class})
    @TestCase
    public void Tc_ApproveRefund_010() {
        Logger.start(true, "宝付审核退款成功");
        try {
            AcquireOrderBean aqb = PayGateWayCallService.initAcquireOrder("70");
            PayGateWayCallService.acquireOrder(aqb);
            PayGateWayCallService.notifyCall(aqb);
            SubmitRefundBean submitrefundBean = PayGateWayCallService.JSubmitRefund(aqb);
            MRefund m=new PayGateWayWapper().selectRefundRequestByTraceId(submitrefundBean.getTraceId()).get(0);
            String refundId = m.getRefundId().toString();
            approverefundBean.setApproveUser("AutoTest");
            List<String> refundIds = new ArrayList<String>();
            refundIds.add(refundId);
            approverefundBean.setRefundIds(refundIds);

            approverefundCall.setData(approverefundBean);
            approverefundCall.callService();
            Logger.verifyEquals("true",approverefundCall.getString("IsSuccess"),"验证IsSuccess");
            Logger.verifyEquals("0",approverefundCall.getString("ErrorCode"),"验证ErrorCode");
            verifyDB(approverefundBean);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }
    @Test
    @Category({P3.class})
    @TestCase
    public void Tc_ApproveRefund_011() {
        Logger.start(true, "花呗审核退款成功");
        try {
            AcquireOrderBean aqb = PayGateWayCallService.initAcquireOrder("9");
            PayGateWayCallService.acquireOrder(aqb);
            PayGateWayCallService.notifyCall(aqb);
            SubmitRefundBean submitrefundBean = PayGateWayCallService.JSubmitRefund(aqb);
            MRefund m=new PayGateWayWapper().selectRefundRequestByTraceId(submitrefundBean.getTraceId()).get(0);
            String refundId = m.getRefundId().toString();
            approverefundBean.setApproveUser("AutoTest");
            List<String> refundIds = new ArrayList<String>();
            refundIds.add(refundId);
            approverefundBean.setRefundIds(refundIds);

            approverefundCall.setData(approverefundBean);
            approverefundCall.callService();
            Logger.verifyEquals("true",approverefundCall.getString("IsSuccess"),"验证IsSuccess");
            Logger.verifyEquals("0",approverefundCall.getString("ErrorCode"),"验证ErrorCode");
            verifyDB(approverefundBean);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }
    private void verifyDB(ApproveRefundBean approverefundBean) throws Exception {
        Logger.comment("验证数据库RefundRequest");
        List<String> refundids = approverefundBean.getRefundIds();
        for (int i = 0; i < refundids.size(); i++) {
        	MRefund m =
                    new PayGateWayWapper().selectRefundRequestByRefundId(refundids.get(i)).get(0);
            Logger.verifyEquals(1, m.getApproveStatus(), "验证RefundRequest表中ApproveStatus字段与返回一致");
        }
    }
}
